/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
import model.Contact;
import view.contact.SmallDisplay;

/**
 *
 * @author gramage
 */
public class MainWindow extends JFrame implements ActionListener {

    private JPanel cont;
    private ArrayList<Contact> sd; //contacts in sd arraylist
    private ArrayList<Contact> ac; //contacts arraylist
    private Menu m;
    private int order;

    /**
     * This class is the main window of the contact book. It contains
     * SmallDisplay of Contact
     *
     * @param al ArrayList that contains contacts
     */
    public MainWindow(ArrayList al) {
        super("Super Address Book");
        this.setSize(300, 600);
        this.setDefaultCloseOperation(MainWindow.EXIT_ON_CLOSE);
        ac = al;
        sd = new ArrayList();
        order = 0;

        //Menu bar
        m = new Menu(ac, this);
        this.setJMenuBar(m);

        // Main Panel
        cont = new JPanel();

        this.add(cont);

        this.setVisible(true);
    }

    /**
     * Refresh contact display, arraylists.
     */
    public void refreshContacts() {
        if (order == 0) {
            for (int i = 0; i < ac.size(); i++) {
            //We roll all the first arraylist (contact)
                //If the contact is in contact arraylist but not in small display arraylist
                //We create new SmallDisplay for this contact
                if (!sd.contains(ac.get(i))) {
                    sd.add(ac.get(i));
                    cont.add(new SmallDisplay(ac.get(i), ac));
                    this.setVisible(true);
                }
            }
            for (int i = 0; i < sd.size(); i++) {
            //We roll all the second arraylist (sd)
                //If the contact is in small display arraylist but not in contact arraylist
                //We delete the SmallDisplay for this contact
                sd.get(i).getSd().refreshDisplay();
                if (!ac.contains(sd.get(i))) {
                    cont.remove(sd.get(i).getSd());
                    this.setVisible(true);
                }
            }
            cont.repaint();
            this.repaint();
        }
        if(order==1){
            
            
            for (int i = 0; i < sd.size(); i++) {
            //We roll all the second arraylist (sd)
                //If the contact is in small display arraylist but not in contact arraylist
                //We delete the SmallDisplay for this contact
                sd.get(i).getSd().refreshDisplay();
                if (!ac.contains(sd.get(i))) {
                    cont.remove(sd.get(i).getSd());
                    this.setVisible(true);
                }
            }
            cont.repaint();
            this.repaint();
        }
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == m.r_date) {
            order = 0;
        }
        if (e.getSource() == m.r_name) {
            order = 1;
        }
    }

}
